<template>
    <div>

        <div class="title">
            <div class="desc">发表论文(选择后原来的直接覆盖了)</div>
            <div>
                <el-button type="primary" @click="handleAdd" :disabled="flag == 3">添加</el-button>
            </div>
        </div>

        <el-table :data="membersList" class="table">

            <el-table-column label="刊物周期" width="150">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.publisherName"
                        placeholder="刊物周期"></el-input>
                    <span v-else>{{ scope.row.publisherName }}</span>
                </template>
            </el-table-column>


            <el-table-column label="论文题目" width="150">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.papersName"
                        placeholder="论文题目"></el-input>
                    <span v-else>{{ scope.row.papersName }}</span>
                </template>
            </el-table-column>




            <el-table-column label="发表时间" width="150">
                <template #default="scope">
                    <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.publicationTime" type="data"
                        value-format="YYYY-MM-DD" placeholder="发表时间" clearable style="width: 100%" />
                    <span v-else>{{ scope.row.publicationTime }}</span>
                </template>
            </el-table-column>

            <el-table-column label="刊号(ISSN)" width="150">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.issnIsbn"
                        placeholder="刊号(ISSN)"></el-input>
                    <span v-else>{{ scope.row.issnIsbn }}</span>
                </template>
            </el-table-column>

            <el-table-column label="维护单位" width="200">
                <template #default="scope">
                    <el-input v-if="activeIndex == scope.$index" v-model="scope.row.completeUnitName"
                        placeholder="维护单位"></el-input>
                    <span v-else>{{ scope.row.completeUnitName }}</span>
                </template>
            </el-table-column>

            <el-table-column label="是否入库" width="150">
                <template #default="scope">
                    <el-select v-if="activeIndex == scope.$index" v-model="scope.row.isIncluded" clearable>
                        <el-option label="是" value="是"></el-option>
                        <el-option label="否" value="否"></el-option>
                    </el-select>
                    <span v-else>{{ scope.row.isIncluded }}</span>
                </template>
            </el-table-column>
            <el-table-column align="right" width="250">
                <template #header>
                    <span>操作</span>
                </template>
                <template #default="scope">
                    <div v-if="activeIndex == scope.$index">

                        <el-button type="success" @click="handleSave">保存</el-button>
                    </div>
                    <div v-else>
                        <el-button type="primary" @click="handleEdit(scope.$index)" :disabled="flag == 3">编辑</el-button>
                        <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                            cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
                            <template #reference>
                                <el-button type="danger" :disabled="flag == 3">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>



        <el-dialog center class="global-dialog" v-model="dialogVisable" width="75%" top="10vh" :show-close="true"
            :close-on-click-modal="false">
            <template #title>
                <div class="my-header">
                    <el-tabs class="demo-tabs">
                        <el-tab-pane label="选择学术论文">

                            <el-col :span="16" align="left" style="margin-bottom:15px">
                                <el-input clearable v-model="search.title" placeholder="论文名称"
                                    style="width: 120px; margin-right: 15px">
                                </el-input>
                                <el-input clearable v-model="search.magazineName" placeholder="期刊名称"
                                    style="width: 120px; margin-right: 15px">
                                </el-input>
                                <!-- <el-select clearable v-model="search.maintainUnitId" placeholder="维护单位">
                                    <el-option :label="item.belongingUnit" :value="item.belongingCode"
                                        :key="item.belongingCode" v-for="item in unitNameList" />
                                </el-select> -->
                                <el-select clearable v-model="search.techClassId" placeholder="技术分类">
                                    <el-option :label="item.itemText" :value="item.itemValue" :key="item.itemValue"
                                        v-for="item in scoreList" />
                                </el-select>
                                <el-button class="ordinary" @click="getTableList">搜索</el-button>
                            </el-col>

                            <div>
                                <el-table :data="tableDate"   v-loading="isLoading" style="width: 100%" class="center-table"
                                    @selection-change="handleSelectionChange">
                                    <el-table-column type="selection" width="55">
                                    </el-table-column>
                                    <ColumnData v-for="column of columns" :key="column.label" :item="column" />
                                </el-table>

                                <!-- <el-pagination :total="pagination.total" :current-page="pagination.page"   page-size="1"
                                        @current-change="currentChange" layout="prev, pager, next ,total,sizes,jumper"
                                        background /> -->
                                <div>

                                </div>
                            </div>

                        </el-tab-pane>

                    </el-tabs>
                </div>
            </template>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="closeVisable">关闭</el-button>
                    <el-button type="primary" @click="choose">选择</el-button>
                </span>
            </template>
        </el-dialog>


    </div>
</template>



<script  lang="ts">

import ColumnData from '@/views/science/components/table/column-data.vue';
import { tips } from "@/views/science/components/tips/tips";
import tableData from '@/views/science/payoffs/thesis/tableHeader.json';
import moment from "moment";

import {
getTable,
} from "@/views/science/api/thesis";
import { reactive, ref, toRefs, watch } from 'vue';

export default {


    components: {
        ColumnData

    },
    emits: {
        dataChange: null,
        paramsChange: null,
    },
    props: {
        members: {
            type: Array,
            required: true,
        },
        scoreList:{
            type: Array,
            required: true,
        },
      // falg  1:新增  2:编辑  3:详情
      flag: {
        type: String,
        default: 1,
      },
                            // <Papers :members="diaForm.papersList" @paramsChange="getParperInfo" :levelList="levelList" ></Papers>


    },



    setup(props, { emit }) {

        let data = reactive({
            unitNameList: [],
            typeList: [],

            tableDate: [],
            temp: [

            ],
            columns: tableData.thesisHeader,
            isLoading: false,
            pagination: { page: 1, pageSize: 10, total: 0 }, //分页
            search: {
                title: "",
                magazineName: "",
                maintainUnitId: "",
                techClassId: "",
            },

        })

        let activeIndex = ref(-1);

        let dialogVisable = ref(false);


        const membersList = ref(props.members)

        watch(props, (newValue, oldValue) => {
            membersList.value = newValue.members
        });


        const closeVisable = () => {

            dialogVisable.value = false

        }

        const handleSelectionChange = (arr) => {


            data.temp = arr.map(item => (
                { publisherName: item.magazineName, papersName: item.title, issnIsbn: '暂无', completeUnitName: item.maintainUnit, isIncluded: '否', publicationTime: item.publicDate, type: 1 }
            )
            )
        }

        //  获取学术论文列表/发送请求/获取数据

        const handleAdd = () => {

            getTableList()
            dialogVisable.value = true
        }

        const getTableList = () => {
            data.isLoading = true,
                getTable({
                    current: data.pagination.page,
                    size: data.pagination.pageSize,
                    ...data.search,
                }).then((res) => {
                    let resData = res.data.data.records;

                    // resData.map((item) => {
                    //     item.publicDate = moment(item.publicDate).format("YYYY-MM-DD");
                    // });
                    resData.map((item) => {
                      if(item.publicDate==null){
                        item.publicDate=='';
                      }else {
                        item.publicDate = moment(item.publicDate).format("YYYY-MM-DD");
                      }
                    });

                    data.tableDate = res.data.data.records;
                    data.pagination.total = res.data.data.total;
                    data.isLoading = false;
                });
        };


        const choose = () => {

            if (data.temp.length == 0) {

                tips("请至少选择一个", "warning");

                return false

            }
            membersList.value = data.temp

            activeIndex.value = -1;

            emit('paramsChange', membersList.value)

            dialogVisable.value = false

        }

        const handleEdit = (index: number) => {

            activeIndex.value = index;
        };

        const handleSave = () => {

            activeIndex.value = -1;

            emit('paramsChange', membersList.value)

        };

        const handleDelete = (index: number) => {
            membersList.value.splice(index, 1);

            emit('paramsChange', membersList.value)
        };

        const  currentChange=(current)=>{

        // debuggur

        }

        return {

            membersList,
            activeIndex,
            dialogVisable,
            choose,
            handleAdd,
            closeVisable,
            handleEdit,
            handleSave,
            handleDelete,
            handleSelectionChange,
            currentChange,
            ...toRefs(data),
            getTableList
        }


    },
}
</script>












<style scoped lang="scss">
.title {
    display: flex;
    justify-content: space-between;

    .desc {
        font-weight: bolder;
    }
}


.title-line {
    color: #409EFF;
}

.dialog-footer {
    display: flex;
    justify-content: end;
}

:deep(.el-dialog__body) {
    display: none;
}

// 滚动条
:deep(.demo-tabs > .el-tabs__content) {
    max-height: 500px;
    overflow-y: scroll;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.table {
    margin: 15px 0;
}
</style>
